<template>
	<view style="padding:20rpx">
		<u-empty v-if="!url" text="您未上传体检报告"></u-empty>
		<view v-if="url" style="padding: 10px; background-color: #fff;">
			<image :src="url" v-if="url" style="width: 100%;" mode="widthFix" @click="pirviewPic(url)"></image>
		</view>

		<view class="flex-row" style="padding: 10px;">
			<button @click="selectFiles" size="mini" type="primary" style="background-color: #ff0101;">
				上传体检报告</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: "",
			}
		},
		onLoad() {
			this.getMyMedicalReport()
		},
		methods: {
			pirviewPic(url) {
				wx.previewImage({
					current: url, // 当前显示图片的http链接
					urls: [url] // 需要预览的图片http链接列表
				})
			},
			getMyMedicalReport() {
				this.$get({
					url: `${this.$Utils.ApiUrl}/healthReport`
				}).then(res => {
					this.url = res.url;
				})
			},
			selectFiles() {
				let that = this;
				wx.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album', 'camera'],
					maxDuration: 30,
					camera: 'back',
					success(res) {
						console.log(res.tempFiles[0].tempFilePath)
						console.log(res.tempFiles[0].size)
						that.uploadFiles(res.tempFiles[0].tempFilePath)
					}
				})
			},
			uploadFiles(fileUrl) {
				this.$get({
					url: `${this.$Utils.ApiUrl}/files/signature`,
					data: {
						dir: 'images/'
					}
				}).then(async (aRes) => {

					const result = await this.$Utils.uploadFilePromise(fileUrl, aRes, 'images');

					// this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
					// 	status: 'success',
					// 	message: '',

					// }))
					this.$put({
						url: `${this.$Utils.ApiUrl}/healthReport`,
						data: {
							url: result
						}
					}).then((uRes) => {
						this.getMyMedicalReport();
					})

				})
			},
		}
	}
</script>

<style>

</style>